package org.example.shop.order.controller;

import org.example.shop.entity.Order;
import org.example.shop.entity.User;
import org.example.shop.entity.Ware;
import org.example.shop.order.service.OrderService;
import org.example.shop.order.service.UserRemoteService;
import org.example.shop.order.service.WareRemoteService;
import org.example.shop.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private WareRemoteService wareRemoteService;

    @Resource
    private UserRemoteService userRemoteService;


    @GetMapping("order/{oid}")
    public ResponseEntity<Order> findOrder(@PathVariable Integer oid) {
        Order order = orderService.findByOid(oid);
        User user = userRemoteService.findByUid(order.getUid());
        Assert.notNull(user, "不存在的用户");
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);
        orderVO.setUsername(user.getUsername());
        Ware ware = wareRemoteService.findByWid(order.getWid());
        Assert.notNull(ware, "不存在的商品");
        orderVO.setWareName(ware.getName());
        return ResponseEntity.ok(orderVO);
    }

    @PostMapping("order/add")
    public ResponseEntity<Order> add(@RequestBody Order order) {
        return ResponseEntity.ok(orderService.save(order));
    }
}
